//
//  YNSSetController.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/23.
//

#import "YNSSetController.h"
#import "YNSSetCommonCell.h"
#import "YNSMineMesNoticeCell.h"
#import "YNSUpdatePassWordController.h"
#import "AILogOutAleartV.h"
#import "AIChageLangV.h"
#import "YNSHomeController.h"
@interface YNSSetController ()<MxNavigationViewDelegate,UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) MxPublicNav *nav;
@property (nonatomic, strong) UITableView *listTab;
@property (nonatomic, strong) UILabel *logoutLab;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, assign) NSInteger selectIndex;
@end

@implementation YNSSetController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectIndex = -1;
    
    NSString *lang = [[NSUserDefaults standardUserDefaults] objectForKey:kLanguageKey];
   if (lang) {
       //设置过语言
       if ([lang isEqualToString:@"zh-Hans"]) {
           self.selectIndex = 0;
       } else {
           self.selectIndex = 1;
       }
   } else {
       NSLocale *currentLocale = [NSLocale currentLocale];
       NSString *languageCode = [currentLocale objectForKey:NSLocaleLanguageCode];
       if ([languageCode isEqualToString:@"zh"]) {
           self.selectIndex = 0;
       } else {
           self.selectIndex = 1;
       }
   }
    [self createUI];
}

- (void)createUI {
    
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.titleLab.text = NSLocalizedString(@"设置", nil);//@"设置";
    self.nav.delegate = self;
    [self.view addSubview:self.nav];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#f5f8fb"];
    
    [self.view addSubview:self.listTab];
    [self.listTab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.offset(0);
        make.top.offset(kNavBarHeight);
        make.height.mas_equalTo(170);
    }];
//    MJWeakSelf
//    self.logoutLab = [[UILabel alloc] init];
//    self.logoutLab.layer.cornerRadius = 3;
//    self.logoutLab.layer.masksToBounds = YES;
//    self.logoutLab.textAlignment = NSTextAlignmentCenter;
//    self.logoutLab.layer.borderWidth = 1;
//    self.logoutLab.layer.borderColor = [UIColor colorWithHexString:@"#CCCCCC"].CGColor;
//    self.logoutLab.text = NSLocalizedString(@"退出登录", nil);//@"退出登录";
//    self.logoutLab.textAlignment = NSTextAlignmentCenter;
//    self.logoutLab.textColor = [UIColor colorWithHexString:@"#999999"];
//    self.logoutLab.font = PFRFont(14);
//    self.logoutLab.userInteractionEnabled = YES;
//    [self.logoutLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
//        [weakSelf logOutClick];
//    }]];
//    [self.view addSubview:self.logoutLab];
//    [self.logoutLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(40);
//        make.bottom.offset(-88);
//        make.centerX.mas_equalTo(self.view);
//        make.width.mas_equalTo(kScreenW-46);
//    }];
    self.listArray = [[NSMutableArray alloc] init];
//    [self getList];
}

- (void)getList {


}

- (void)logOutClick {

    AILogOutAleartV *view = [[AILogOutAleartV alloc] init];
    zhPopupController *_zh_popupController = [[zhPopupController alloc] initWithView:view size:view.bounds.size];
    _zh_popupController.presentationStyle = zhPopupSlideStyleTransform;
    _zh_popupController.layoutType = zhPopupLayoutTypeCenter;
    _zh_popupController.dismissOnMaskTouched = YES;
    [_zh_popupController showInView:self.view.window duration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut bounced:NO completion:nil];
    view.cancleClick = ^{
        [_zh_popupController dismiss];
    };
    view.sureClick = ^{
        [_zh_popupController dismiss];
    };
}


- (UITableView *)listTab {
    
    if (nil == _listTab) {
        _listTab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listTab.backgroundColor = [UIColor whiteColor];
        _listTab.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTab.delegate = self;
        _listTab.dataSource = self;
        _listTab.rowHeight = 60.0;
        _listTab.showsVerticalScrollIndicator = NO;
        _listTab.showsHorizontalScrollIndicator= NO;
        [_listTab registerClass:[YNSSetCommonCell class] forCellReuseIdentifier:NSStringFromClass([YNSSetCommonCell class])];
        [_listTab registerClass:[YNSMineMesNoticeCell class] forCellReuseIdentifier:NSStringFromClass([YNSMineMesNoticeCell class])];
        
        if (@available(iOS 11.0, *)) {
            _listTab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (@available(iOS 15.0, *)) {
            _listTab.sectionHeaderTopPadding = 0;
        }
    }
    return _listTab;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        YNSSetCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YNSSetCommonCell class])];
        [cell reloadCellWithTitle:NSLocalizedString(@"中文(简体)", nil) select:(self.selectIndex == indexPath.row)];
        return cell;
    }
    YNSSetCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YNSSetCommonCell class])];
    [cell reloadCellWithTitle:@"English" select:(indexPath.row == self.selectIndex)];
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectIndex == indexPath.row) {
        return;
    }
    self.selectIndex = indexPath.row;
    [self.listTab reloadData];
    NSString *language = @"";
    if (indexPath.row == 0) {
        language = kChinese;
    } else {
        language = kEnglish;
    }
    
    if (language.length>0 && ![[[NSUserDefaults standardUserDefaults] objectForKey:kLanguageKey] isEqualToString:language]) {
        [NSBundle setLanguage:language];
        [[NSUserDefaults standardUserDefaults] setObject:language forKey:kLanguageKey];
        
        YNSHomeController *ctl = [[YNSHomeController alloc] init];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:ctl];
        UIWindow *window = [UIApplication sharedApplication].delegate.window;
        window.rootViewController = navi;
        [window makeKeyAndVisible];
    }
//    NSString *type = @"";
//    if (indexPath.row == 0) {
//        type = @"zh";
//    } else {
//        type = @"en";
//    }
//    MJWeakSelf
//    [[DCHttpClient shareClient] requestWithPath:@"api/user/sswitchLanguage" params:@{@"language" : type} httpMethod:DCHttpRequestPost sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
//        NSDictionary * dict = [responseObject mj_JSONObject];
//        if([dict[@"code"] intValue]==1){
//
//        } else {
//            [weakSelf.view xjmakeToast:dict[@"msg"]];
//        }
//    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
//       
//    }];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 50)];
    headView.backgroundColor =  [UIColor colorWithHexString:@"#f5f8fb"];
    UILabel *titleLab = [[UILabel alloc] init];
    [headView addSubview:titleLab];
    titleLab.text = NSLocalizedString(@"语言", nil);//@"语言";
    titleLab.textColor = [UIColor colorWithHexString:@"#999999"];
    titleLab.font = PFRFont(16);
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.mas_equalTo(headView);
    }];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
